package com.leetcode.leetcode3;

import java.util.Arrays;

/**
 * 轮转数组
 *
 * @author luhd
 * @date 2023-08-11
 */
public class Practice189 {
    /**
     * 给定一个整数数组 nums，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。
     *
     * 输入: nums = [1,2,3,4,5,6,7], k = 3
     * 输出: [5,6,7,1,2,3,4]
     * 解释:
     * 向右轮转 1 步: [7,1,2,3,4,5,6]
     * 向右轮转 2 步: [6,7,1,2,3,4,5]
     * 向右轮转 3 步: [5,6,7,1,2,3,4]
     */
    public static void main(String[] args) {
        int[] nums = {1,2,3,4,5,6,7};
        int k = 3;
        rotate(nums, k);
        System.out.println(Arrays.toString(nums));
    }

    public static void rotate(int[] nums, int k) {
        int cycle = k % nums.length;
        int[] copyArr = Arrays.copyOf(nums, nums.length);
        for (int i = nums.length-cycle; i < nums.length; i++) {
            nums[i+cycle-nums.length] = copyArr[i];
        }
        for (int i = 0; i < nums.length-cycle; i++) {
            nums[i+cycle] = copyArr[i];
        }
        /*LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < nums.length; i++) {
            linkedList.add(nums[i]);
        }
        for (int i = 0; i < k; i++) {
            Integer peek = linkedList.peekLast();
            linkedList.removeLast();
            linkedList.addFirst(peek);
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = linkedList.get(i);
        }*/
    }
}